﻿package src.components 
{	
	/**
	* 
	* Reliability project
	* -----------------------
	* @author  Dmitrij Boycov
	* @mail    dmitry.boycov@gmail.com
	* @date    April 27, 2010
	* @version 1
	* 
	*/
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	
	import src.util.Utils;
	
	public class Menu extends MovieClip{
		
		private var mWidth: Number = 50;
		private var mHeight:Number = 100;
		private var mAlpha: Number = 1;
		private var mColour = 0x321517;
		//private var mColour = 0x878787;		
		private var glowColour = 0x3d4146;
		private var mX:Number = 100;
		private var mY:Number = 100;
		private var buttons:Array;
		private var padding:Number = 7;
		private var mFond:Sprite;
		
		public function Menu(buttons:Array) {
			super();
			this.buttons = buttons; 
			var i:Number;
			var h:Number = padding;
			var w:Number = 0;
			for (i = 0; i < this.buttons.length; i++ ) {				
				if (this.buttons[i].width > w) {
					w = this.buttons[i].width;
				}				
				h = h + this.buttons[i].height + this.padding;				
			}
			mWidth = w + 2*this.padding;
			mHeight = h + this.padding;
			this.mFond = createBackground();			
			///////////////////////////////////////
			h = padding;
			for (i = 0; i < this.buttons.length; i++ ) {
				this.mFond.addChild(this.buttons[i]);
				this.buttons[i].x = padding;
				this.buttons[i].y = h;
				h = h + this.buttons[i].height + this.padding;
			}
			this.mFond.buttonMode = true;			
			this.mFond.useHandCursor = true;	
			addChild(this.mFond);
			this.width = mWidth;
			this.height = mHeight;
			this.x = mX;
			this.y = mY;
		}	
		
		private function createBackground():Sprite {
			var promptBackground:Sprite = new Sprite();			
			//	Create a background for the prompt
			var ellipseSize:int = 10;
			promptBackground.graphics.lineStyle(1);
			promptBackground.graphics.beginFill(mColour);
			promptBackground.graphics.drawRoundRect(0, 0, mWidth, mHeight, ellipseSize, ellipseSize);
			promptBackground.graphics.endFill();
			//
			promptBackground.filters = [Utils.getGlowFilter(glowColour), Utils.getDropShadowFilter(glowColour)];
			promptBackground.alpha = mAlpha;			
			var actualPrompt:Sprite = new Sprite();		
			actualPrompt.addChild(promptBackground);			
			actualPrompt.addEventListener(MouseEvent.MOUSE_DOWN, doStartDrag);
			actualPrompt.addEventListener(MouseEvent.MOUSE_UP, doStopDrag);
			return actualPrompt;
		}
		
		/*
		* 	Drag and drop
		*/
		private static function doStartDrag(event:MouseEvent):void {
			if (event.target is Sprite) event.currentTarget.startDrag();
			
		}
		private static function doStopDrag(event:MouseEvent):void {
			if (event.target is Sprite) event.currentTarget.stopDrag();			
		}
		///
	}
	
}